﻿#region using statements

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using NGen;
using NGen.Managers.GUI;
using NGen.Managers.GameState;

using Nuclex.UserInterface;
using Nuclex.UserInterface.Controls;
using Nuclex.UserInterface.Controls.Desktop;

using TowerDefense.Misc;
using TowerDefense.States;

#endregion

namespace TowerDefense.GUILayouts
{
	/// <summary>
	/// 	Gui used in the setup state
	/// </summary>
	public class GUIElementSetupStateLayout : GUIElement
	{
		#region local fields

		//this button is used as a background, is not active
		private readonly ButtonControl m_backButton = new ButtonControl();

		private readonly ButtonControl m_background = new ButtonControl();

		private readonly ListControl m_levelList = new ListControl();

		private readonly LabelControl m_levelListLabel = new LabelControl();

		private readonly ListControl m_locationList = new ListControl();

		private readonly LabelControl m_locationListLabel = new LabelControl();

		private readonly ButtonControl m_startButton = new ButtonControl();

		/// <summary>
		/// 	The image of the selected map
		/// </summary>
		private Texture2D m_mapImage;

		/// <summary>
		/// 	Object to store the options selected
		/// </summary>
		private GameOptions m_options = new GameOptions();

		#endregion

		#region GUIElement implementation

		/// <summary>
		/// 	_Initialize this gui
		/// </summary>
		protected override void _Initialize() {
			//get the options service if it exists, create it otherwise
			m_options = GameEngine.GetService<GameOptions>() ?? new GameOptions();

			//create the gui 
			m_background.Bounds = new UniRectangle(90, 90, 640, 360);
			m_background.Enabled = false;

			m_locationList.Bounds = new UniRectangle(145, 165, 125, 63);
			m_locationList.Children.Clear();
			m_locationList.SelectionMode = ListSelectionMode.Single;
			m_locationList.Items.Add("Grass");
			m_locationList.Items.Add("Sand");
			m_locationList.Items.Add("Snow");
			m_locationList.SelectedItems.Add(0);
			m_locationList.SelectionChanged += _LevelListSelectionChanged;
			m_locationList.Children.Add(m_locationListLabel);
			m_locationListLabel.Text = "Select Location";
			m_locationListLabel.Bounds = new UniRectangle(-125, 30, 0, 0);

			m_levelList.Bounds = new UniRectangle(145, 45, 125, 63);
			m_levelList.Children.Clear();
			m_levelList.SelectionMode = ListSelectionMode.Single;
			m_levelList.Items.Add("Beginner");
			m_levelList.Items.Add("Intermediate");
			m_levelList.Items.Add("Expert");
			m_levelList.SelectedItems.Add(0);
			_LevelListSelectionChanged(this, null);
			m_levelList.SelectionChanged += _LevelListSelectionChanged;
			m_levelList.Children.Add(m_levelListLabel);
			m_levelListLabel.Text = "Select Map";
			m_levelListLabel.Bounds = new UniRectangle(-125, 30, 0, 0);

			m_backButton.Bounds = new UniRectangle(25, 290, 100, 40);
			m_backButton.Text = "Back";
			m_backButton.Pressed += _BackButtonPressed;
			m_startButton.Bounds = new UniRectangle(170, 290, 100, 40);
			m_startButton.Text = "Start";
			m_startButton.Pressed += _StartButtonPressed;

			_Children.Add(m_background);
			m_background.Children.Add(m_levelList);
			m_background.Children.Add(m_locationList);

			m_background.Children.Add(m_startButton);
			m_background.Children.Add(m_backButton);
		}

		/// <summary>
		/// 	Called when the start button is clicked
		/// </summary>
		private void _StartButtonPressed(object p_sender, EventArgs p_e) {
			//add the options to the services so they are available
			if(GameEngine.GetService<GameOptions>() != null) {
				GameEngine.GameServices.RemoveService(typeof(GameOptions));
			}
			GameEngine.GameServices.AddService(typeof(GameOptions), m_options);

			//go to the play state
			GameEngine.GetService<IManagerGameState>().ChangeState(new GameStatePlay());
		}

		/// <summary>
		/// 	Called when the back button is clicked
		/// </summary>
		private void _BackButtonPressed(object p_sender, EventArgs p_e) {
			//go to the menu state
			GameEngine.GetService<IManagerGameState>().ChangeState(new GameStateMenu());
		}

		/// <summary>
		/// 	Called whenever the list selection is changed
		/// </summary>
		private void _LevelListSelectionChanged(object p_sender, EventArgs p_e) {
			//get the name of the map based on the selection
			switch(m_levelList.SelectedItems[0]) {
				case 0:
					m_options.MapName = "Beginner_";
					break;
				case 1:
					m_options.MapName = "Intermediate_";
					break;
				case 2:
					m_options.MapName = "Expert_";
					break;
			}
			switch(m_locationList.SelectedItems[0]) {
				case 0:
					m_options.MapName += "Grass";
					break;
				case 1:
					m_options.MapName += "Sand";
					break;
				case 2:
					m_options.MapName += "Snow";
					break;
			}

			//load the new map image
			m_mapImage = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\Level\\" + m_options.MapName);
		}

		/// <summary>
		/// 	Update this gui
		/// </summary>
		/// <param name = "p_gameTime">snapshot of timing values</param>
		protected override void Update(GameTime p_gameTime) {
			//no updates are necessary
		}

		/// <summary>
		/// 	_Draw this gui
		/// </summary>
		/// <param name = "p_gameTime">snapshot of timing values</param>
		protected override void _Draw(GameTime p_gameTime) {
			//draw the map image
			GameEngine.SpriteBatch.Draw(m_mapImage, new Rectangle((int) m_background.Bounds.Left.Offset + 310, (int) m_background.Bounds.Top.Offset + 30, 300, 300), Color.White);
		}

		/// <summary>
		/// 	Cleanup this gui
		/// </summary>
		protected override void _Cleanup() {}

		#endregion
	}
}